<?php

class UnderwoodMenu extends AbstractSingleton {

    /**
     * @return UnderwoodMenu
     */
    public static function getInstance() { return parent::doGetInstance(get_class()); }

    private $hPages;
    protected function __construct() {

        $this->hPages = Settings::config('pages')->getAll();

        foreach ($this->hPages as $path => &$hPage) {

            if (empty($hPage['controller']))
                throw new Exception("Invalid section `$path`");

            if (empty($hPage['title']))
                $hPage['title'] = 'untitled';
        }
    }

    /**
     * @return array
     */
    public function getPageConfigByPath($path) {
        return isset($this->hPages[$path]) ?
            $this->hPages[$path] :
            false;
    }

    /**
     * @return array
     */
    public function getRootItems() {

        $hPages = array_filter($this->hPages, create_function('array $hPage','
            return empty($hPage["parent"]);
        '));

        return array_map(create_function('array $hPage', 'return $hPage["title"];'), $hPages);
    }
}

?>